package logic

import (
	"context"
	"database/sql"
	"fmt"
	"net/http"
	"zero_six/goods/internal/model"

	"zero_six/goods/internal/svc"
	"zero_six/goods/pb/goods"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveLogic {
	return &SaveLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SaveLogic) Save(in *goods.Request) (*goods.Response, error) {
	// todo: add your logic here and delete this line

	res, err := l.svcCtx.GoodsModel.Insert(context.Background(), &model.Goods{
		GoodsName:  in.GoodsName,
		GoodsPrice: sql.NullFloat64{float64(in.GoodsPrice), true},
		Status:     in.Status,
		GoodsImg:   sql.NullString{in.GoodsImg, true},
		Detail:     sql.NullString{in.Detail, true},
	})
	if err != nil {
		return &goods.Response{
			Code: http.StatusOK,
			Msg:  "添加失败",
		}, nil
	}
	fmt.Println("新添加的id：")
	fmt.Println(res.LastInsertId())

	return &goods.Response{
		Code: http.StatusOK,
		Msg:  "添加成功",
	}, nil
}
